import Vuex from 'vuex'
import completion from '../net/completion'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    year: new Date().getFullYear(),
    userAdcd: '',
    adcd: '',
    adcdName: [],
    adcdId: [],
    energyType: '',
    gridHead: [
      { name: 'GSJC', value: '厂家'},
      { name: 'JHCL_Q', value: '确村数'},
      { name: 'JHSL_Q', value: '确户数'},
      { name: 'startWork', value: '开工总数(户/台)'},
      { name: 'startWorkRate', value: '开工率'},
      { name: 'finishRate', value: '完成率'},
      { name: 'uninstall', value: '未安装(户/台)'},
      { name: 'underConstruction', value: '施工中(户/台)'},
      { name: 'install', value: '安装完毕(户/台)'},
      { name: 'YXSL', value: '正式运行'}
    ],
    gridData: [],
    energyTypeData: [
      { name: '全部', value: ''},
      { name: '煤改气', value: '010903'},
      { name: '煤改电', value: '010901'}
    ],
    sort: 1,
    sortData: [
      { name: '开工总数', value: 1 },
      { name: '确户数', value: 2 },
      { name: '安装完毕', value: 3 },
      { name: '完成率', value: 4 },
      { name: '开工率', value: 5 }
    ]
  },
  mutations: {
    changeYear (state, value) {
      state.year = value
    },
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeAdcd (state, value) {
      state.adcd = value
    },
    changeAdcdName (state, value) {
      state.adcdName = value
    },
    changeAdcdId (state, value) {
      state.adcdId = value
    },
    changeEnergyType (state, value) {
      state.energyType = value
    },
    changeGridHead (state, value) {
      state.gridHead = value
    },
    changeGridData (state, value) {
      state.gridData = value
    },
    changeSort (state, value) {
      state.sort = value
    }
  },
  actions: {
    getLedger ({commit, state}, vm) {
      vm.$notice.loading.show()
      completion.getCompletion({
        nd: state.year,
        quyxzqhdm: state.adcd,
        types: '',
        sorts: state.sort,
        nylxdm: state.energyType
      }).then(res => {
        res.forEach(value => {
          value.startWork = value.SBSL_H_ALL + '/' + value.SBSL_ALL
          value.startWorkRate = value.JHWCL + '%'
          value.finishRate = value.AZWBL + '%'
          value.uninstall = value.SBSL11_H + '/' + value.SBSL11
          value.underConstruction = value.SBSL12_H + '/' + value.SBSL12
          value.install = value.SBSL13_H + '/' + value.SBSL13
        })
        commit('changeGridData', res)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
